package com.ddyx.util;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**
 * @ClassName:  FileUpload   
 * @Description:图片上传示例 
 * @author: JiangShuai 
 * @date:   2018年1月4日 上午10:14:47     
 * @Copyright: 2018 dingdian.parllay.cn Inc. All rights reserved. 
 * 注意：本内容仅限于叮当一下(北京)信息技术有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
public class FileUpload {
	
	/**
	 * @Title: fileUpload   
	 * @Description: 单图片上传  
	 * @author: JiangShuai
	 * @param: @param req
	 * @param: @param response
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: String 本地路径
	 */
	public static String fileUpload(
			HttpServletRequest request,HttpServletResponse response) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
		System.out.println("---------------fileupload------------start--------->");
		MultipartHttpServletRequest req=(MultipartHttpServletRequest) request;
		Iterator<String> itr = req.getFileNames();
		String adress= PropertiesUtil.getProperty("QRCode.logo");
		String imgurl="";
		while(itr.hasNext()){
			String str = itr.next();
			CommonsMultipartFile multipartFile = (CommonsMultipartFile)req.getFile(str);
			String fileName = multipartFile.getOriginalFilename();   //原文件名
			MultipartFile file1 = req.getFile(str);
			String pic="";
			if(file1.getSize()!=0){
	   			 pic = UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf("."));
	   			 try {
	   				String savePath =PropertiesUtil.getProperty("QRCode.logo");
	   				File test = new File(savePath+pic);
	   				if (!test.exists()) {
	   					test.mkdirs();
	   				}
	   				//传送  失败就抛异常
	   				file1.transferTo(test); 
	   			 } catch (Exception e) {
	   				e.printStackTrace();
	   			 }
			}else{
			}
			imgurl+=adress+pic+",";
		}
		System.out.println("-----------------------------fileupload--end---------------------------------------");
		return imgurl.substring(0, imgurl.length()-1);
	}
	
	/**
	 * @Title: fileListUpload   
	 * @Description: 多图片上传 
	 * @author: JiangShuai
	 * @param: @param saveAdress
	 * @param: @param req
	 * @param: @param response
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: Map<String,Object>
	 */
	public Map<String, Object> fileListUpload(
			@RequestParam(value = "saveAdress", required = false) String saveAdress,
			HttpServletRequest req,HttpServletResponse response) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map=new HashMap<String, Object>();
		System.out.println("---------------------------saveAdress----->"+saveAdress);
		MultipartHttpServletRequest request=(MultipartHttpServletRequest) req;
		Iterator<String> itr = request.getFileNames();
		String name="";
		String imgurl="";
		String url="";
		String adress= PropertiesUtil.getProperty(saveAdress+"url");
		while(itr.hasNext()){
			String str = itr.next();
			CommonsMultipartFile multipartFile = (CommonsMultipartFile)request.getFile(str);
			String fileName = multipartFile.getOriginalFilename();   //原文件名
			MultipartFile file1 = request.getFile(str);
			String pic=null;
			if(file1.getSize()!=0){
	   			 pic = UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf("."));
	   			 try {
	   				String savePath =PropertiesUtil.getProperty(saveAdress)+"/";
	   				File test = new File(savePath+pic);
	   				if (!test.exists()) {
	   					test.mkdirs();
	   				}
	   				file1.transferTo(test); //传送  失败就抛异常
	   			 } catch (Exception e) {
	   				e.printStackTrace();
	   				map.put("code", "1001");
	   				map.put("msg", "上传失败!");
	   				return map;
	   			 }
			}else{
				map.put("code", "1001");
				map.put("msg", "上传失败!");
   				return map;
			}
			name+=pic+",";
			imgurl+=PropertiesUtil.getProperty("imgurlpre")+adress+pic+",";
			url+=adress+pic+",";
		}
		System.out.println("-----------------------------------------------------------------------------------------");
		map.put("code", "1000");
		map.put("msg", "上传成功!");
		map.put("imgurl",imgurl.substring(0, imgurl.length()-1));
		map.put("url", url.substring(0, url.length()-1));
		map.put("name", name.substring(0, name.length()-1));
		return map;
	}
	
}
